using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.WCFProxy
{
	/// <summary>
	/// The service implementation for Customer object
	/// </summary>
	public partial class CustomerService : ICustomerService
	{
		#region ICustomerService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public CustomerDTO SelectByPrimaryKey(string customerid)
		{
			return DataService.Instance.GetCustomer(customerid);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<CustomerDTO> list)
		{
			bool retval = true;
			foreach (CustomerDTO customer in list)
			{
				retval &= this.Persist(customer);
			}
			return retval;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(CustomerDTO dto)
		{
			return (DataService.Instance.PersistCustomer(dto) != null);
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<CustomerDTO> RunSelect(string linq)
		{
			List<CustomerDTO> retVal = new List<CustomerDTO>();
			retVal.AddRange(DataService.Instance.GetCustomerListByLINQ(linq));
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<CustomerDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			return DataService.Instance.GetCustomerListPaged(pageIndex, pageSize, ascending, sortColumn, linq);
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<CustomerDTO> RunSelect()
		{
			List<CustomerDTO> retVal = new List<CustomerDTO>();
			DataServiceClient ds = DataService.Instance;
			retVal.AddRange(ds.GetCustomerList());
			return retVal;
		}

		/// <summary>
		/// Return a list of related CustomerCustomerDemo objects
		/// </summary>
		public List<CustomerCustomerDemoDTO> GetCustomerCustomerDemoList(CustomerDTO item)
		{
			List<CustomerCustomerDemoDTO> retval = new List<CustomerCustomerDemoDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.CustomerId = \"" + item.CustomerId + "\"";
			retval.AddRange(ds.GetCustomerCustomerDemoListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return a list of related Order objects
		/// </summary>
		public List<OrderDTO> GetOrderList(CustomerDTO item)
		{
			List<OrderDTO> retval = new List<OrderDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.CustomerId = \"" + item.CustomerId + "\"";
			retval.AddRange(ds.GetOrderListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return a list of items by the City field
		/// </summary>
		public List<CustomerDTO> SelectByCity(CustomerDTO item, string city)
		{
			List<CustomerDTO> retval = new List<CustomerDTO>();
			DataServiceClient ds = DataService.Instance;
			retval.AddRange(ds.GetCustomerByCityField(city));
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(CustomerDTO item)
		{
			DataServiceClient ds = DataService.Instance;
			return ds.DeleteCustomer(item);
		}

		#endregion
	}
}
